package com.company.string;

import java.util.HashMap;

/**
 * @author jrue
 * @description https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
 * @date 2019/11/15 14:33
 * 给定一个字符串，请你找出其中不含有重复字符的 最长子串 的长度。
 */
public class LengthOfLongestSubstring {

    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("dvdf"));
    }

    public static int lengthOfLongestSubstring(String s) {
        if (s.length() == 0) return 0;
        int left = 0;
        int max = 0;
        HashMap<Character,Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {

            if (map.containsKey(s.charAt(i))) {
                left = Math.max(left, map.get(s.charAt(i)) + 1);
            }

            map.put(s.charAt(i), i);

            max = Math.max(max, i - left + 1);
        }

        return max;
    }

}
